
import 'package:flutter/material.dart';
import 'package:flutter_foodybite/models/profile.dart';

import '../Global.dart';

class ProfileChangeNotifier extends ChangeNotifier {
  Profile get _profile => Global.profile;

  Profile get profile => _profile;

  @override
  void notifyListeners() {
    Global.saveProfile(); //保存Profile变更
    super.notifyListeners(); //通知依赖的Widget更新
  }
}

class LocaleModel extends ProfileChangeNotifier {
  // 获取当前用户的APP语言配置Locale类，如果为null，则语言跟随系统语言
  Locale? getLocale() {
    if (profile.locale == null)
      return null;
      // return Locale('zh', 'CN'); //'en', 'US'
    var t = profile.locale!.split("_");
    return Locale(t[0], t[1]);
  }

  // 获取当前Locale的字符串表示
  String? get applocal => profile.locale;

  // 用户改变APP语言后，通知依赖项更新，新语言会立即生效
  set locale(String locale) {
    if (locale != _profile.locale) {
      profile.locale = locale;
      notifyListeners();
    }
  }

  void changeLauguage() async {
    var l = applocal;
    debugPrint("changeLauguage old language=$l");
    if (l == "zh_CN") {
      locale = "en_US";
    } else if (l == "en_US") {
      locale = "zh_CN";
    } else {
      locale = "en_US";
    }
  }
}